package com.awenhui.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.hibernate.validator.internal.constraintvalidators.bv.time.futureorpresent.FutureOrPresentValidatorForJapaneseDate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试类
 *
 * @author yuxf
 * @version 1.0
 * @date 2020/11/2 9:14
 */
@Controller
@RequestMapping("/test")
public class TestController {

    @GetMapping("/index")
    public String index() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //获取request,response,session
        HttpServletRequest request = requestAttributes.getRequest();
        HttpSession session = request.getSession();
        HttpServletResponse response = requestAttributes.getResponse();
        RequestContextHolder.currentRequestAttributes();

        return "test/index";
    }

    @GetMapping("restData/{age}")
    @ResponseBody
    public Object restData(String name, @PathVariable String age) {
        Map<String, String> map = new HashMap<>();
        map.put("name", name);
        map.put("age", age);
        map.put("sex", "0");
        return map;
    }


    @GetMapping("/getForObject")
    @ResponseBody
    public Object getForObject() {
        RestTemplate restTemplate = new RestTemplate();
        String url1 = "http://localhost:8802/abc/test/restData/{1}?name=于新方";
        JSONObject forObject1 = restTemplate.getForObject(url1, JSONObject.class, 17);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("j1", forObject1);
        return jsonObject;
    }


    @PostMapping("postRestData")
    @ResponseBody
    public String postRestData(String name) {
        Map<String, String> map = new HashMap<>();
        map.put("name", name);
        map.put("sex", "0");
        return JSON.toJSONString(map);
    }

    @ResponseBody
    @PostMapping("postForObject")
    public Object postForObject() {
        RestTemplate restTemplate = new RestTemplate();
        String url1 = "http://localhost:8802/abc/test/postRestData";
        //简单提交键值对
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("name", "胡春燕");
        String s = restTemplate.postForObject(url1, map, String.class);

        //提交json
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "卢桂玲");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Arrays.asList(new MediaType[]{MediaType.APPLICATION_JSON}));
        HttpEntity<String> httpEntity = new HttpEntity<>(jsonObject.toString(), headers);
        restTemplate.postForObject(url1, httpEntity, String.class);

        return s;
    }


    /**
     * @param <T>
     * @return
     */
    public <T> T postNameValueForObject(String url, HashMap<String, String> params, Class<T> responseType) {
        RestTemplate restTemplate = new RestTemplate();
        //简单提交键值对
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        params.keySet().stream().forEach(r -> map.add(r, params.get(r)));
        return restTemplate.postForObject(url, map, responseType);
    }

    //提交json
    public <T> T postJsonForObject(String url, String json, Class<T> responseType) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Arrays.asList(new MediaType[]{MediaType.APPLICATION_JSON}));
        HttpEntity<String> httpEntity = new HttpEntity<>(json, headers);
        return restTemplate.postForObject(url, httpEntity, responseType);
    }


}
